package com.winit.deploy.server;

import com.winit.common.util.ObjectToString;
import com.winit.core.server.SpringBootServer;
import com.winit.deploy.bean.WinitSpringBootInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * Created by wangjian on 16/8/11.
 */
public class WinitSpringBootServer extends SpringBootServer {

    static final Logger logger = LoggerFactory.getLogger(WinitSpringBootServer.class);

    private WinitSpringBootInfo info;

    public WinitSpringBootServer(String ip, String userName, Integer port, WinitSpringBootInfo info) throws Exception {
        super(ip, userName, port, info.getServerPath(), info.getWarName());
        this.info = info;
    }

    public WinitSpringBootInfo getInfo() {
        return this.info;
    }

    /**
     * 备份应用
     * 1.进入工程存放目录
     * 2.判断工程文件夹是否存在  存在继续  不存在退出
     * 3.将原工程打包至备份目录
     */
    public void doBakUp() throws Exception {
        String serverPath = this.info.getServerPath();
        String warName = this.info.getWarName();
        String backUpPath = this.info.getBackUpPath();
        String backUpDir = this.info.getBackUpDir();

        if (StringUtils.isEmpty(serverPath)
                || StringUtils.isEmpty(warName)
                || StringUtils.isEmpty(backUpPath)
                || StringUtils.isEmpty(backUpDir)) {
            logger.error("WinitSpringBootServer:备份应用,参数存在空值." + ObjectToString.getString(this.info));
            throw new Exception("备份应用,参数存在空值");
        }

        StringBuffer cmd = new StringBuffer();
        cmd.append("ls ").append(serverPath).append(" | grep ^").append(warName).append(".jar$ && cd ").append(serverPath).append(" && tar -zcf ").append(backUpPath).append(" *  --exclude=*.log");
        this.doExec(cmd.toString());
        //删除旧备份文件
        if (!StringUtils.isEmpty(backUpDir) && !StringUtils.isEmpty(warName)) {
            cmd = new StringBuffer();
            cmd.append("source /etc/profile && ls ").append(backUpDir).append(" | grep ^").append(warName).append("- && cd ").append(backUpDir)
                    .append(" && rm -rf `ls -t ").append(warName).append("-* | tail -n+4`");
            this.doExec(cmd.toString());
        }
    }

    public boolean doInstall() throws Exception {
        String serverPath = this.info.getServerPath();
        String warName = this.info.getWarName();
        String jarTempDir = this.info.getJarTempDir();

        if (StringUtils.isEmpty(serverPath)
                || StringUtils.isEmpty(warName)
                || StringUtils.isEmpty(jarTempDir)) {
            logger.error("WinitSpringBootServer:安装程序,参数存在空值." + ObjectToString.getString(this.info));
            throw new Exception("安装程序,参数存在空值");
        }
        StringBuffer cmd = new StringBuffer();
        cmd.append("mv ").append(jarTempDir).append("/").append(warName).append(".tgz ").append(serverPath)
                .append("/ && cd ").append(serverPath).append(" && tar -zxf ").append(warName)
                .append(".tgz && rm -rf ").append(serverPath).append("/").append(warName).append(".tgz");
        this.initHandler();
        this.doExec(this.handler, cmd.toString());

        if (!this.handler.isSuccess()) {
            logger.error("WinitSpringBootServer:安装程序失败.返回:" + this.handler.getMessage());
            throw new Exception("安装程序失败." + this.handler.getMessage());
        }
        return true;
    }

    public void doRollBack() throws Exception {
        String serverPath = this.info.getServerPath();
        String warName = this.info.getWarName();
        String backUpPath = this.info.getBackUpPath();

        if (StringUtils.isEmpty(serverPath)
                || StringUtils.isEmpty(warName)
                || StringUtils.isEmpty(backUpPath)) {
            logger.error("WinitSpringBootServer:执行回滚,参数存在空值." + ObjectToString.getString(this.info));
            throw new Exception("执行回滚,参数存在空值");
        }
        //1.判断回滚包是否存在
        this.initHandler();
        this.doExec(this.handler, "ls " + backUpPath);
        if (StringUtils.isEmpty(this.handler.getMessage())
                || this.handler.getMessage().indexOf("No such file or directory") != -1) {
            throw new Exception("执行回滚,备份包不存在." + backUpPath);
        }

        StringBuffer cmd = new StringBuffer();
        cmd.append("tar -zxf ").append(backUpPath).append(" -C ").append(serverPath).append("/");
        this.initHandler();
        this.doExec(this.handler, cmd.toString());

        if (!this.handler.isSuccess()) {
            logger.error("WinitSpringBootServer:代码回滚失败.返回:" + this.handler.getMessage());
            throw new Exception("代码回滚失败.返回:" + this.handler.getMessage());
        }
    }
}
